package com.mspbots.common;

import org.springframework.util.ObjectUtils;

import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * com.mspbots.common.TimeZoneUtils
 *
 * @author Jason
 * @date 2019/11/28
 **/
public class TimeZoneUtils {

    public static final String DATE_TIME_ES = "MM/dd/yy HH:mm";

    public static ZonedDateTime zoneTimeNow(String zone) {
        return ZonedDateTime.of(LocalDateTime.now(), ZoneId.of(zone));
    }

    public static String formatNow() {
        return format(ZonedDateTime.now());
    }

    public static ZonedDateTime utcToZone(LocalDateTime localDateTime, String tzStr) {
        return ZonedDateTime.of(localDateTime, ZoneId.of("UTC")).withZoneSameInstant(ZoneId.of(tzStr));
    }

    public static LocalDateTime offset(LocalDateTime localDateTime, String offsetStr) {
        if (!ObjectUtils.isEmpty(offsetStr) && offsetStr.contains(":")) {
            String[] arr = offsetStr.split(":");
            Integer hour = Integer.parseInt(arr[0].trim());
            Integer minuts = 0;
            if (arr.length > 1) {
                minuts = Integer.parseInt(arr[1].trim());
            }
            if (hour.toString().startsWith("-")) {
                return localDateTime.plusHours(hour).minusMinutes(minuts);
            } else {
                return localDateTime.plusHours(hour).plusMinutes(minuts);
            }
        } else {
            return localDateTime;
        }
    }

    public static ZonedDateTime zoneToUtc(LocalDateTime localDateTime, String tzStr) {
        return ZonedDateTime.of(localDateTime, ZoneId.of(tzStr)).withZoneSameInstant(ZoneId.of("UTC"));
    }

    public static String formatUtcToZone(ZonedDateTime zoneDateTime, String tzStr) {
        return format(utcToZone(zoneDateTime.toLocalDateTime(), tzStr));
    }

    /**
     * @param localDateTime
     * @param tzStr
     * @return
     */
    public static String formatUtcToZone(LocalDateTime localDateTime, String tzStr) {
        return format(utcToZone(localDateTime, tzStr));
    }

    /**
     * @param tzStr
     * @return String
     */
    public static String formatZoneTimeNow(String tzStr) {
        return formatUtcToZone(ZonedDateTime.now(), tzStr);
    }

    /**
     * @param zoneDateTime
     * @param pattern
     * @return String
     */
    public static String format(ZonedDateTime zoneDateTime, String pattern) {
        return zoneDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static String format(ZonedDateTime zoneDateTime) {
        return zoneDateTime.format(DateTimeFormatter.ofPattern(DATE_TIME_ES));
    }

    public static String formatUtcToZone(String zoneTimeStr, String tzStr) {
        return formatUtcToZone(ZonedDateTime.parse(zoneTimeStr), tzStr);
    }


    public static void main(String[] args) {

//        ZonedDateTime zdt = ZonedDateTime.of(LocalDate.now(),LocalTime.now(),ZoneId.of("-05:00"));
//        System.out.println(zdt);
//        System.out.println(zdt.toLocalDateTime());
//
//        System.out.println(TimeZoneUtils.utcToZone(zdt.toLocalDateTime(), "-05:00").toLocalDateTime());
//
//        System.out.println(TimeZoneUtils.zoneToUtc(zdt.toLocalDateTime(), "-05:00").toLocalDateTime());


        LocalDateTime logTime1 = LocalDateTime.of(LocalDate.now(), LocalTime.of(22, 00, 0, 0));
        System.out.println(logTime1);


        ZonedDateTime logTime2 = ZonedDateTime.of(logTime1, ZoneId.of("-05:00"));
        System.out.println(logTime2);

        LocalDateTime logTime3 = TimeZoneUtils.zoneToUtc(logTime2.toLocalDateTime(), "-05:00").toLocalDateTime();
        System.out.println(logTime3);


//        System.out.println(TimeZoneUtils.utcToZone(LocalDateTime.of(LocalDate.now(), LocalTime.MAX), "-06:00").toLocalDateTime());
//
//        System.out.println(TimeZoneUtils.zoneToUtc(LocalDateTime.of(LocalDate.now(), LocalTime.MAX), "-06:00").toLocalDateTime());
//        ZonedDateTime zbj = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
//        System.out.println(zbj);
//        ZonedDateTime zbj2 = ZonedDateTime.now(ZoneId.of("-07:00"));
//        System.out.println(zbj2);
//        System.out.println(zbj2.withFixedOffsetZone().format(DateTimeFormatter.ISO_DATE_TIME));


//        ZonedDateTime zutc = ZonedDateTime.of(LocalDateTime.now(), ZoneId.of("UTC+00:00"));
//        System.out.println(zutc.format(DateTimeFormatter.ISO_DATE_TIME));
//        ZonedDateTime zbj4 = zutc.withZoneSameInstant(ZoneId.of("-08:00"));
//        System.out.println(zbj4.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
//
//        System.out.println(format(ZonedDateTime.parse("2019-11-27T10:50:47Z")));

//        System.out.println(formatNow());
//        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
//        System.out.println(LocalDateTime.now().format(DateTimeFormatter.ISO_ZONED_DATE_TIME));

//        String test = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(TimeZoneUtils.utcToZone(LocalDateTime.of(LocalDate.now(), LocalTime.MIN), "-06:00"));
//        System.out.println(test);


//        ZonedDateTime z = ZonedDateTime.of(ZonedDateTime.now().toLocalDate(), LocalTime.MIN, ZoneId.of("-06:00"));
//
//        System.out.println(ZonedDateTime.of(ZonedDateTime.now().toLocalDate(), LocalTime.MIN, ZoneId.of("-06:00")));
//        System.out.println(z);
//        System.out.println(z.toLocalDate());
//        System.out.println(ZonedDateTime.of(z.toLocalDate(), LocalTime.MIN, ZoneId.of("-06:00")));
//        System.out.println(TimeZoneUtils.formatUtcToZone(ZonedDateTime.of(z.toLocalDate(), LocalTime.MIN, ZoneId.of("-06:00")), "-06:00"));
//        System.out.println(TimeZoneUtils.utcToZone(LocalDateTime.of(LocalDate.now(), LocalTime.MIN), "-06:00").toLocalDateTime());
//        System.out.println(z.toLocalDateTime());

//        ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("-06:00"));
//        System.out.println(zonedDateTime.toLocalDate());
//        System.out.println(zonedDateTime.toLocalDateTime());
//        System.out.println(zonedDateTime.withZoneSameInstant(ZoneId.of("-06:00")));
//        System.out.println(ZonedDateTime.now(ZoneId.of("-06:00")));
//        System.out.println(ZonedDateTime.now());
//
//        System.out.println(TimeZoneUtils.zoneToUtc(LocalDateTime.of(LocalDate.now(), LocalTime.MIN), "-06:00").toLocalDateTime());
    }


}
